package foodmaterial.config;

import foodmaterial.filter.CustomizeAbstractSecurityInterceptor;
import foodmaterial.filter.ValidateCodeFilter;
import foodmaterial.security.CustomizeAuthenticationSuccessHandler;
import foodmaterial.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private PermissionService userService ;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint ;
    @Autowired
    private CustomizeAuthenticationSuccessHandler authenticationSuccessHandler ;
    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler ;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler ;
    @Autowired
    private SessionInformationExpiredStrategy sessionInformationExpiredStrategy ;
    @Autowired
    FilterInvocationSecurityMetadataSource securityMetadataSource;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler ;
    @Autowired
    private CustomizeAbstractSecurityInterceptor securityInterceptor ;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter(authenticationFailureHandler);
        http.cors().and().csrf().disable();
        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class);
        http.authorizeRequests().antMatchers("/api/init/showCompany").permitAll()
                .antMatchers("/index.html").permitAll()
                .antMatchers("/*/cl").permitAll()
                .antMatchers("/*/gl").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers("/api/system/verify").permitAll()
                .anyRequest().authenticated()
                .and().formLogin().loginProcessingUrl("/api/system/userLogin").permitAll().successHandler(authenticationSuccessHandler).
                failureHandler(authenticationFailureHandler)
                .and().logout().permitAll().logoutSuccessHandler(logoutSuccessHandler).
                deleteCookies("JSESSIONID")
                .and().exceptionHandling().accessDeniedHandler(accessDeniedHandler)
                .authenticationEntryPoint(authenticationEntryPoint)
                .and().sessionManagement().maximumSessions(1).
                expiredSessionStrategy(sessionInformationExpiredStrategy);
        http.addFilterBefore(securityInterceptor, FilterSecurityInterceptor.class);
    }

}
